package cn.songhaiqing.walle.help;

import android.content.Context;
import android.provider.ContactsContract;
import android.text.format.DateFormat;
import android.util.Log;

import java.util.Calendar;

import cn.songhaiqing.walle.bledemo.MainActivity;

public class CommandHelper {
    public static final int DEFAULT_ALARM_HOUR = 0;
    public static final int DEFAULT_ALARM_MINUTE = 0;
    public static final int DEFAULT_ALARM_REPEAT = 0;

    public static byte[] getOpenPhoneState() {
        byte[] value = new byte[20];
        value[0] = 47;
        value[1] = 1;
        value[2] = 1;
        return value;
    }

    public static byte[] getDateTimeValue() {
        byte[] value = new byte[20];
        Calendar cal = Calendar.getInstance();
        int year = cal.get(1);
        int month = cal.get(2) + 1;
        int day = cal.get(5);
        int hour = cal.get(11);
        int minute = cal.get(12);
        int second = cal.get(13);
        Log.i("ParserHelper", "date time:" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
        value[0] = 1;
        value[1] = (byte) (year / 100);
        value[2] = (byte) (year % 100);
        value[3] = (byte) month;
        value[4] = (byte) day;
        value[5] = (byte) hour;
        value[6] = (byte) minute;
        value[7] = (byte) second;
        value[8] = 0;
        value[9] = 24;
        return value;
    }

    public static byte[] getLostModeValue(int type) {
        byte[] value = new byte[20];
        value[0] = 35;
        value[1] = (byte) type;
        return value;
    }

    public static byte[] getGoalValue(Context context) {
        int stepGoal;
        int size;

        byte[] value = new byte[20];
        //
        //   value[0] = MqttWireMessage.MESSAGE_TYPE_UNSUBACK;
        value[1] = 1;
        return value;
    }

    public static byte[] getActivityCountValue() {
        byte[] value = new byte[20];
        value[0] = 16;
        return value;
    }

    public static byte[] getActivityValue(int sn, int hour) {
        byte[] value = new byte[20];
        value[0] = 19;
        value[1] = (byte) sn;
        value[2] = (byte) hour;
        return value;
    }

    public static byte[] getSleepValue(int sn, int hour) {
        byte[] value = new byte[20];
        value[0] = 17;
        value[1] = (byte) sn;
        value[2] = (byte) hour;
        return value;
    }

    public static byte[] getSleepValueNew(int sn, int hour) {
        byte[] value = new byte[20];
        value[0] = 18;
        value[1] = (byte) sn;
        value[2] = (byte) hour;
        return value;
    }

    public static byte[] getDayModeValue(Context context) {

        byte[] value = new byte[20];
        value[0] = 2;

        return value;
    }

    public static byte[] getAlarmValue(Context context) {
        int hour_1 = 0;
        int minute_1 = 0;
        int repeat_1 = 0;
        int hour_2 = 0;
        int minute_2 = 0;
        int repeat_2 = 0;
        int hour_3 = 0;
        int minute_3 = 0;
        int repeat_3 = 0;

        byte[] value = new byte[20];
        value[0] = 3;
        value[1] = (byte) hour_1;
        value[2] = (byte) minute_1;
        value[3] = (byte) repeat_1;
        value[4] = (byte) hour_2;
        value[5] = (byte) minute_2;
        value[6] = (byte) repeat_2;
        value[7] = (byte) hour_3;
        value[8] = (byte) minute_3;
        value[9] = (byte) repeat_3;
        return value;
    }

    public static byte[] getAlarmValue2(Context context) {
        int hour_4 = 0;
        int minute_4 = 0;
        int repeat_4 = 0;
        int hour_5 = 0;
        int minute_5 = 0;
        int repeat_5 = 0;
        int hour_6 = 0;
        int minute_6 = 0;
        int repeat_6 = 0;
        int hour_7 = 0;
        int minute_7 = 0;
        int repeat_7 = 0;
        int hour_8 = 0;
        int minute_8 = 0;
        int repeat_8 = 0;

        byte[] value = new byte[20];
        value[0] = 42;
        value[1] = (byte) hour_4;
        value[2] = (byte) minute_4;
        value[3] = (byte) repeat_4;
        value[4] = (byte) hour_5;
        value[5] = (byte) minute_5;
        value[6] = (byte) repeat_5;
        value[7] = (byte) hour_6;
        value[8] = (byte) minute_6;
        value[9] = (byte) repeat_6;
        value[10] = (byte) hour_7;
        value[11] = (byte) minute_7;
        value[12] = (byte) repeat_7;
        value[13] = (byte) hour_8;
        value[14] = (byte) minute_8;
        value[15] = (byte) repeat_8;
        return value;
    }

    public static byte[] getProfileValue(Context context) {
        double height = 175.0d;
        double weight = 75.0d;
        int gender = 1;
        int age = 25;

        return null;
    }

    public static byte[] getCameraValue(int type) {
        byte[] value = new byte[20];
        value[0] = 80;
        value[1] = 1;
        if (type == 0) {
            value[2] = 81;
        } else if (type == 1) {
            value[2] = 80;
        }
        return value;
    }

    public static byte[] getCallValue(Context context, String number) {
        if ((DeviceHelper.isSH09() || DeviceHelper.isSH09u() || DeviceHelper.isI7PLUS() || DeviceHelper.isI10() || DeviceHelper.isI3()) && number != null) {
            //  String number2 = ParserHelper.phoneNumberFilter(number);
            byte[] value = new byte[20];
            value[0] = 32;
            value[1] = 0;

            return value;
        }
        byte[] value2 = new byte[20];
        value2[0] = 32;
        value2[1] = 0;
        if (!DeviceHelper.isH03PRO()) {
            return value2;
        }
        value2[3] = 2;
        return value2;
    }

    public static byte[] getCallTerminationValue() {
        byte[] value = new byte[20];
        value[0] = 32;
        value[1] = 1;
        return value;
    }

    public static byte[] getSMSValue(String number) {
        if ((DeviceHelper.isSH09() || DeviceHelper.isSH09u() || DeviceHelper.isI7PLUS() || DeviceHelper.isI10() || DeviceHelper.isI3()) && number != null) {

            return null;
        }
        byte[] value2 = new byte[20];
        value2[0] = 33;
        value2[1] = 0;
        value2[2] = 0;
        return value2;
    }

    public static byte[] getZoonValue() {
        byte[] value = new byte[20];
        value[0] = 65;
        value[1] = 0;
        return value;
    }

    public static byte[] getBoundValue(byte type) {
        byte[] value = new byte[20];
        value[0] = 15;
        value[1] = 2;
        value[2] = type;
        //  Log.e("zhuantaiXXXXXXXXXXXXXXXXXXX", "" + ((int) type));
        return value;
    }

    public static byte[] getBoundValue() {
        byte[] value = new byte[20];
        value[0] = 15;
        value[1] = 0;
        return value;
    }

    public static byte[] getVersionValue() {
        byte[] value = new byte[20];
        value[0] = 112;
        return value;
    }

    public static byte[] getBatteryValue() {
        byte[] value = new byte[20];
        value[0] = 48;
        return value;
    }

    public static byte[] getTouchVibrationvalue(boolean isTouchVibration, int time) {
        byte[] value = new byte[20];
        value[0] = 84;
        value[1] = 1;
        if (isTouchVibration) {
            value[2] = (byte) time;
        } else {
            value[2] = 0;
        }
        return value;
    }

    public static byte[] getUnitValue(int type) {
        byte[] value = new byte[20];
        value[0] = 85;
        value[1] = 1;
        value[2] = (byte) type;
        return value;
    }

    public static byte[] getTimeFormatValue(Context context) {
        int time_format;
        byte[] value = new byte[20];
        boolean is24Hour = DateFormat.is24HourFormat(context);
        Log.i("sync", "timeFormat:" + is24Hour);
        if (is24Hour) {
            time_format = 0;
        } else {
            time_format = 1;
        }
        //  value[0] = MqttWireMessage.MESSAGE_TYPE_PINGRESP;
        value[1] = (byte) time_format;
        return value;
    }

    public static byte[] getHeartValue(byte type) {
        byte[] value = new byte[20];
        value[0] = 86;
        value[1] = type;
        if (type == 4) {
            boolean isOpen = HeartHelper.getIsHeartAlarmOn();
            int high = HeartHelper.getHeartAlarmValueHigh();
            int low = HeartHelper.getHeartAlarmValueLow();
            boolean isShow = HeartHelper.getIsHeartShow();
            if (isOpen) {
                value[2] = 1;
            } else {
                value[2] = 0;
            }
            value[3] = (byte) high;
            value[4] = (byte) low;
            if (isShow) {
                value[5] = 1;
            } else {
                value[5] = 0;
            }

        }
        return value;
    }

    public static byte[] getRRiValue() {
        byte[] value = new byte[20];
        value[0] = 94;
        value[1] = 0;
        return value;
    }

    public static byte[] setRRiValue(int T, int t) {
        int size;
        byte[] value = new byte[20];
        value[0] = 94;
        value[1] = 1;
        value[2] = (byte) T;
        String hrvBinary = Integer.toBinaryString(t);
        if (hrvBinary != null && (size = hrvBinary.length()) < 16) {
            for (int i = 0; i < 16 - size; i++) {
                hrvBinary = "0" + hrvBinary;
            }
        }
        String hrvHigh = hrvBinary.substring(0, 8);
        String hrvLow = hrvBinary.substring(8, 16);
        value[4] = (byte) Integer.parseInt(hrvHigh, 2);
        value[3] = (byte) Integer.parseInt(hrvLow, 2);
        return value;
    }

    public static byte[] getBloodPressure(int sbp, int dbp) {
        byte[] value = new byte[20];
        value[0] = 87;
        value[1] = 0;
        value[2] = (byte) sbp;
        value[3] = (byte) dbp;
        return value;
    }
}
